package cc.linker.steplink.crm.task;


import cc.linker.steplink.crm.commons.enums.IntegralEnums;
import cc.linker.steplink.crm.commons.lock.RedisLock;
import cc.linker.steplink.crm.merchanth5.dao.VipCustomerLogDao;
import cc.linker.steplink.crm.merchanth5.dao.integral.IntegralH5Dao;
import cc.linker.steplink.crm.merchanth5.service.CustomerIntegralService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Component
public class VipLogTask {

    @Autowired
    private VipCustomerLogDao vipCustomerLogDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private CustomerIntegralService customerIntegralService;
    @Autowired
    private IntegralH5Dao integralH5Dao;


    /**
     * 会员卡库存回归
     */
    @RedisLock("cancelVipLimit")
    @Scheduled(cron = "2 */10 * * * ?")
    public void cancelEnrollRecord(){
        List<Map<String, Object>> unpayList = vipCustomerLogDao.getUnpayList();
//        LOGGER.info("退会员卡库存任务"+ JSON.toJSONString(unpayList));
        if(null != unpayList && unpayList.size()>0){
            for(Map<String, Object> order:unpayList){
                String vipId = order.get("vipId").toString();
                String limitKey = "{vip}vip:limit_num_"+vipId;
                LOGGER.info("退会员卡库存任务"+ limitKey);
                stringRedisTemplate.opsForValue().increment(limitKey,1);
                vipCustomerLogDao.delVipLog(order.get("logId").toString());
            }
        }
    }


    /**
     * 积分商城会员卡取消支付回归
     */
    @RedisLock("cancelIntegralVipOrder")
    @Scheduled(cron = "2 */5 * * * ?")
    public void cancelIntegralVipOrder(){
        List<Map<String, Object>> unpayList = vipCustomerLogDao.getUnpayOrderList();
        if(null!=unpayList && unpayList.size()>0){
            for (Map<String, Object> order : unpayList) {
                if(Integer.parseInt(order.get("integral").toString()) > 0){
                    //积分回归
                    this.customerIntegralService.addIntegralCustomerLog(order.get("customerId").toString(),"取消兑换权益卡", IntegralEnums.customerScore.toString(),order.get("integral").toString(),IntegralEnums.other.toString(),order.get("orgId").toString());
                    //增加日志
                    Map<String, Object> log = new HashMap<String, Object>();
                    log.put("logId", UUID.randomUUID().toString());
                    log.put("customerId", order.get("customerId"));
                    log.put("scoreValue",order.get("integral"));
                    log.put("scoreType", 6);
                    log.put("actionRemark", "取消兑换权益卡,回退"+order.get("integral")+"积分");
                    integralH5Dao.addIntegralLog(log);
                }
                //回归库存
                String integralVipId = order.get("integralVipId").toString();
                String integralOptionid = order.get("integralOptionid").toString();
                Map<String, Object> iParam = new HashMap<>();
                iParam.put("id",integralVipId);
                Map<String, Object> integralVipInfo = vipCustomerLogDao.getIntegralVipInfo(iParam);
                String options = integralVipInfo.get("options").toString();
                List<Map<String,Object>> optionsList = (List)JSON.parse(options);
                for(Map<String,Object> op:optionsList){
                    if(integralOptionid.equals(op.get("optionId").toString())){
                        int goodsCount = Integer.valueOf(op.get("goodsCount").toString());
                        op.put("goodsCount",goodsCount+1);
                    }
                }
                Map<String ,Object> tmp = new HashMap<>();
                tmp.put("options",JSON.toJSONString(optionsList));
                tmp.put("integralVipId",integralVipId);
                vipCustomerLogDao.saveIntegralGoods(tmp);
                //删除订单
                vipCustomerLogDao.delVipLog(order.get("logId").toString());
            }
        }
    }

}
